﻿using DevExpress.Utils;
using DevMy.PublicSetUp;
using MDIDemo.Model;
using MDIDemo.PublicClass;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevMy.Vou
{
    public partial class Form_Project_picture : Form_BaseAlone
    {
        public Form_Project_picture(string FormText)
        {
            InitializeComponent();
            SetComponent(FormText);
        }
        private string mainId;
        private WaitDialogForm waitDialogForm;
        private Class_MySqlDataBase class_MySqlDataBase;
        private Class_LookUpEdit class_LookUpEdit;
        private void SetComponent(string FormText)
        {
            base.SetComponent(FormText, "I0021");
            base.SetGridDataSource(null);
            base.GetGridPage().PageSize = 50;
            base.EnableViewButton(false);
            base.EnableOutButton(false);

            LookUpEditColumnModel[] lookUpEditColumnModels = new LookUpEditColumnModel[2];
            lookUpEditColumnModels[0] = new LookUpEditColumnModel("projectName", "项目名称");
            lookUpEditColumnModels[1] = new LookUpEditColumnModel("id", "项目编号");


            Class_PublicMethod class_PublicMethod = new Class_PublicMethod();
            Class_DataBaseConDefault class_DataBaseConDefault = new Class_DataBaseConDefault();
            class_DataBaseConDefault = class_PublicMethod.FromXmlToDefaultValueObject<Class_DataBaseConDefault>("DataBaseDefaultValues");

            class_MySqlDataBase = new Class_MySqlDataBase(class_DataBaseConDefault.dataSourceUrl
                , class_DataBaseConDefault.dataBaseName
                , class_DataBaseConDefault.dataSourceUserName
                , class_DataBaseConDefault.dataSourcePassWord
                , class_DataBaseConDefault.Port);
            DataTable dataTable = new DataTable();
            dataTable = class_MySqlDataBase.GetDataTable("SELECT id,projectName FROM vou_project ORDER BY createDate DESC");
            class_LookUpEdit = new Class_LookUpEdit();
            class_LookUpEdit.SetLookUpEdit(this.lookUpEdit1, "projectName", "id", lookUpEditColumnModels, "项目信息");
            this.lookUpEdit1.Properties.DataSource = dataTable;
            dataTable.Dispose();
        }
        private string GetMainId()
        {
            return null;
        }
        public override int AddData()
        {
            int index = -1;
            if (mainId == null)
            {
                MessageBox.Show("请选择项目！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return index;
            }
            Form_Project_PictureBase form_Project_PictureBase = new Form_Project_PictureBase();
            form_Project_PictureBase.SetWinType(1);
            form_Project_PictureBase.id = null;
            form_Project_PictureBase.Ini();
            form_Project_PictureBase.Text = this.Text;

            form_Project_PictureBase.mainId = mainId;
            if (form_Project_PictureBase.ShowDialog() == DialogResult.OK)
            {
                string Sql = string.Format(@"Insert Into vou_project_picture(id,mainId,fileName,fileStream,sortNo,remark)
                Values (?id,?mainId,?fileName,?fileStream,?sortNo,?remark)");
                MySqlParamerModel[] mySqlParamerModels = new MySqlParamerModel[6];
                mySqlParamerModels[0] = new MySqlParamerModel("?id", form_Project_PictureBase.projectPictureModel.id, MySqlDbType.VarChar);
                mySqlParamerModels[1] = new MySqlParamerModel("?mainId", form_Project_PictureBase.projectPictureModel.mainId, MySqlDbType.VarChar);
                mySqlParamerModels[2] = new MySqlParamerModel("?fileName", form_Project_PictureBase.projectPictureModel.fileName, MySqlDbType.VarChar);
                mySqlParamerModels[3] = new MySqlParamerModel("?fileStream"
                    , form_Project_PictureBase.projectPictureModel.fileStream
                    , MySqlDbType.Blob);
                mySqlParamerModels[4] = new MySqlParamerModel("?sortNo", form_Project_PictureBase.projectPictureModel.sortNo, MySqlDbType.Int32);
                mySqlParamerModels[5] = new MySqlParamerModel("?remark", form_Project_PictureBase.projectPictureModel.remark, MySqlDbType.VarChar);

                index = class_MySqlDataBase.ExecuteSqlByParamer(Sql, mySqlParamerModels);
            }
            form_Project_PictureBase.Dispose();
            return index;
        }
        public override int EditData(object id)
        {
            int index = -1;
            Form_Project_PictureBase form_Project_PictureBase = new Form_Project_PictureBase();
            form_Project_PictureBase.SetWinType(0);
            form_Project_PictureBase.id = id.ToString();
            form_Project_PictureBase.Ini();
            form_Project_PictureBase.Text = this.Text;
            if (form_Project_PictureBase.ShowDialog() == DialogResult.OK)
            {

                string Sql = string.Format(@"Update vou_project_picture
                Set fileName='{1}',sortNo={2},remark='{3}' Where id='{0}'"
                , form_Project_PictureBase.projectPictureModel.id
                , form_Project_PictureBase.projectPictureModel.fileName
                , form_Project_PictureBase.projectPictureModel.sortNo
                , form_Project_PictureBase.projectPictureModel.remark);
                index = class_MySqlDataBase.ExecuteSql(Sql);
            }
            form_Project_PictureBase.Dispose();
            return index;
        }
        public override void ViewData(object id)
        {
            MessageBox.Show(id.ToString());
        }

        public override int DeleteData(object id)
        {
            string Sql = string.Format(@"Delete From vou_project_picture
                Where id='{0}'", id.ToString());
            //class_MySqlDataBase.ExecuteSql(Sql);
            //Sql = string.Format("delete from vou_project where id = '{0}'", id.ToString());
            return class_MySqlDataBase.ExecuteSql(Sql);
        }

        public override void ToExcel(SaveFileDialog saveFileDialog)
        {
        }

        public override void ToWord(SaveFileDialog saveFileDialog)
        {
            base.ToWord(saveFileDialog);
        }

        public override void Finder(int PageNo)
        {
            if (this.mainId == null)
            {
                //MessageBox.Show("请选择项目！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            waitDialogForm = new WaitDialogForm("正在玩命加载中......", "温馨提示");

            DataSet dataSet = this.GetFinder(PageNo, base.GetGridPage().PageSize, mainId);
            base.SetGridDataSource(dataSet.Tables[0]);
            base.GetGridPage().SetRowCount(int.Parse(dataSet.Tables[1].Rows[0][0].ToString()));
            waitDialogForm.Close();
        }
        private DataSet GetFinder(int PageNo, int PageSize, string Address)
        {
            string SelectSql = @"SELECT id
                , fileName
                , sortNo
                , remark";
            string FormSql = "FROM vou_project_picture";
            string WhereSql = null;
            string GroupSql = null;
            string HavingSql = null;
            string OrderSql = "ORDER BY sortNo";
            string TotalFieldName = "*";

            if (Address != null && Address.Trim().Length > 0)
            {
                WhereSql = string.Format("Where mainId = '{0}'", Address.Trim());
            }

            PageSqlModel pageSqlModel = class_MySqlDataBase.GetSql(SelectSql
            , FormSql
            , WhereSql
            , GroupSql
            , HavingSql
            , OrderSql
            , PageNo
            , PageSize
            , TotalFieldName);

            return class_MySqlDataBase.GetPageDataSet(pageSqlModel);
        }

        private void Form_Project_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (waitDialogForm != null)
            {
                waitDialogForm.Dispose();
            }
            class_MySqlDataBase.CloseSqlConnection();
        }

        private void lookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {
            mainId = this.lookUpEdit1.EditValue.ToString();
            Finder(base.GetGridPage().GetPageNo());
        }
    }
}
